//
//  IDNumberViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-19.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "IDNumberViewController.h"
#import "PopLoadMsg.h"
#import "PopMsg.h"
#import "PhotographViewController.h"
@interface IDNumberViewController () <UITextFieldDelegate>
{
    UITextField *_fieldIDNum;
    UIButton *_btnLast;
    UIButton *_btnNext;
}
@end

@implementation IDNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle:@"注册-身份验证"];
    [self configView];
}

- (void)configView
{
    //身份证号label
    UILabel *nameLabel = [self createLabelFrame:CGRectMake(20*W, SCREEN_HEIGHT/2-85*H, SCREEN_WIDTH-40*W, 30*H) title:@"身份证号" font:[UIFont systemFontOfSize:26]];
    [self.view addSubview:nameLabel];
    
    //身份证号输入框
    _fieldIDNum = [self createTextFieldFrame:CGRectMake(20*W, CGRectGetMaxY(nameLabel.frame)+20*H, SCREEN_WIDTH-40*W, 35*H) title:nil placeholder:@"请填写身份证号码"];
    _fieldIDNum.returnKeyType = UIReturnKeyDone;
    _fieldIDNum.delegate = self;
    [self.view addSubview:_fieldIDNum];
    
    //上一步按钮
    _btnLast = [self addLastBtnTarget:self action:@selector(btnLastClicked:)];
    [self enableButton:_btnLast];
    
    //下一步按钮
    _btnNext = [self addNextBtnTarget:self action:@selector(btnNextClicked:)];
    [self disableButton:_btnNext];
}

#pragma mark - 网络请求
- (void)verifyIDNumRequest
{
    PopLoadMsg *loadmsg = [PopLoadMsg new];
    [loadmsg delayLoadImages];
    
#warning  这是注册三级联动进行调整修改
    
    
    
    
    NSString *phoneNum = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNo"];
    
    
    
//    phoneNum  = @"13764807768";
    NSDictionary *paramdic = @{@"loginId":phoneNum,@"cardId":_fieldIDNum};
    NSString *url = [urlstring stringByAppendingString:@"verifyIDCardNum.do"];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:url withParam:paramdic success:^(AFHTTPRequestOperation *op,id responseObject){
        [loadmsg delayImagesRemove];
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSLog(@"dic = %@", dic);
        NSInteger flag = [[dic objectForKey:@"flag"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        if(flag == 0){
            //验证成功
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:_fieldIDNum.text forKey:@"idcardNum"];
            PhotographViewController *photoCtrl = [PhotographViewController new];
            [self.navigationController pushViewController:photoCtrl animated:YES];
        }
        else {
            [PopMsg showMessage:msg Lasttime:1.0f];
        }
        
    }failure:^(AFHTTPRequestOperation *op,NSError *error){
        [loadmsg delayImagesRemove];
        [PopMsg showMessage:@"网络异常，请重试" Lasttime:1.0f];
    }];
}

#pragma mark - 点击事件
//上一步，返回上一个界面
- (void)btnLastClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//下一步，验证身份证号是否可用,可用则进入身份证照片上传界面
- (void)btnNextClicked:(id)sender
{
    if (_fieldIDNum.text.length == 18) {
        [self verifyIDNumRequest];
    }
    else {
        [PopMsg showMessage:@"身份证号不正确" Lasttime:1.0f];
    }
}

#pragma mark- UITextFieldDelegate
//收起键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_fieldIDNum resignFirstResponder];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSString *tempStr = [_fieldIDNum.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (tempStr.length == 0) {
        [self disableButton:_btnNext];
    }
    else {
        [self enableButton:_btnNext];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_fieldIDNum resignFirstResponder];
    return YES;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
